package com.itheima.service;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.excption.MyException;
import com.itheima.pojo.PmsBrand;
import com.itheima.pojo.PmsProduct;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.vo.PageResultVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PmsBrandService {
    //商品的api
    @DubboReference
    private PmsBrandApi pmsBrandApi;
    @DubboReference
    private PmsProductApi pmsProductApi;


    /**
     * 创建品牌
     * 品牌名称不能重复
     *
     * @param
     */
    public void create(PmsBrand pmsBrand) {

        Boolean flag = pmsBrandApi.create(pmsBrand);

        if (!flag) {
            //品牌名重复
            throw new MyException("品牌名重复,添加失败");
        }

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    public void deleteByIds(Integer[] ids) {
        //删除之前 判断品牌下面是否有商品
        for (Integer id : ids) {
            List<PmsProduct> pmsProducts = pmsProductApi.findByBrandId(id);
            if (pmsProducts.size() != 0) {
                //说明就有商品
                throw new MyException("您选中的品牌下,存在商品,无法删除,删除失败!");
            }
        }
        pmsBrandApi.delete(ids);
    }

    /**
     * 分页查询品牌
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    public PageResultVo<PmsBrand> findPages(Integer pageNum, Integer pageSize, String keyword) {
        IPage<PmsBrand> page = pmsBrandApi.findPages(pageNum, pageSize, keyword);

        PageResultVo<PmsBrand> pageResultVo = new PageResultVo<>();
        pageResultVo.setPageNum(pageNum);
        pageResultVo.setPageSize(pageSize);
        Long total = page.getTotal();
        pageResultVo.setTotal(total.intValue());
        Long pages = page.getPages();
        pageResultVo.setTotalPages(pages.intValue());
        List<PmsBrand> records = page.getRecords();
        pageResultVo.setList(records);
        //BeanUtils.copyProperties(records,pageResultVo.getList());
        return pageResultVo;
    }

    /**
     * 获取全部品牌
     *
     * @return
     */
    public List<PmsBrand> listAll() {
        return pmsBrandApi.listAll();
    }


    /**
     * 批量修改厂家制造状态
     *
     * @param factoryStatus
     * @param ids
     */
    public void updateFactoryStatus(Integer factoryStatus, Integer[] ids) {

        pmsBrandApi.update(factoryStatus, ids);
    }


    /**
     * 批量修改显示状态
     *
     * @param
     * @param ids
     */
    public void updateShowStatus(Integer showStatus, Integer[] ids) {
        pmsBrandApi.updateShow(showStatus, ids);
    }


    /**
     * 注意品牌名重复问题
     *
     * @param pmsBrand
     */
    public void updateById(PmsBrand pmsBrand) {
        //先进行查询
        PmsBrand pmsBrandApiByID = pmsBrandApi.findByID(pmsBrand.getId());


        if (pmsBrand.getName().equals(pmsBrandApiByID.getName())) {
            //广告名未修改 ,直接不用判断 直接修改
            //throw new MyException("新的广告名和旧的广告名一样哦");
            pmsBrandApi.updateByIdNo(pmsBrand);
            return;
        }
        Boolean aBoolean = pmsBrandApi.updateById(pmsBrand);
        if (!aBoolean) {
            throw new MyException("广告名已经有了哦,请换一个名字");
        }
    }


    /**
     * 根据id 查询
     *
     * @param id
     * @return
     */
    public PmsBrand findById(Integer id) {

        PmsBrand pmsBrandApiByID = pmsBrandApi.findByID(id.longValue());
        return pmsBrandApiByID;
    }
}
